package delsig

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"line-follower-bot/cache"
	"line-follower-bot/models"
	"line-follower-bot/pkj"
	"strconv"
	"time"
)

func Dl(c *gin.Context) {
	name := c.PostForm("name")
	passes := c.PostForm("passes")

	var user models.User
	err := models.DB.Where("name=?", name).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}

	if user.Passes != pkj.Md5(passes) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码不正确",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func Pl(c *gin.Context) {

}
func Qx(c *gin.Context) {

}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))

	offer := (page - 1) * size

	var count int64
	models.DB.Model(&models.User{}).Count(&count)

	var user []models.User
	redis_key := "redis_po"
	JsonData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if JsonData == "" {
		models.DB.Order("id desc").Offset(offer).Limit(size).Find(&user)
		SnappyData, _ := json.Marshal(&user)
		cache.Rdb.Set(cache.Ctx, redis_key, SnappyData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(JsonData), &user)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"user":  user,
		},
	})
	return
}
